package com.person.jc.web.buy;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.person.jc.common.Constants;
import com.person.jc.entity.TsUser;
import com.person.jc.service.buy.BsBuyApplyService;
import com.person.jc.utils.DateUtils;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.SessionKit;
import com.person.jc.utils.Utils;

public class BsBuyApplyController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		setAttr("startDate",DateUtils.formatDate(DateUtils.addMonths(new Date(), -1), "yyyy-MM-dd"));
		
		setAttr("endDate",DateUtils.getDate());
		render("main.jsp");
	}
	
	public void list(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		map.put("optUser", tsUser.getUserId());
		List<Record> list = BsBuyApplyService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String buyId = getPara("buyId");
		String view = getPara("view");
		if(Utils.isNotEmpty(buyId)){
			Record buy = BsBuyApplyService.me.getBuyInfo(buyId);
			List<Record> details = BsBuyApplyService.me.getBuyDetail(buyId);
			setAttr("obj", buy);
			setAttr("details", details);
		}else{
			setAttr("reqDate", DateUtils.getDateTime());
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	
	@Before(Tx.class)
	public void save(){
		Map<String, Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String buyId = String.valueOf(map.get("buyId"));
		TsUser tsUser = (TsUser)SessionKit.get().getAttribute(Constants.LOGIN_USER);
		map.put("buyUser", tsUser.getUserId());
		if(Utils.isEmpty(buyId)){
			buyId = BsBuyApplyService.me.save( map);
		}else{
			BsBuyApplyService.me.update(map);
		}
		BsBuyApplyService.me.deleteDetail(buyId);
		String[] productName = getParaValues("productName");
		String[] model = getParaValues("model");
		String[] num = getParaValues("num");
		String[] unitPrice = getParaValues("unitPrice");
		if(productName.length != 0){
			Object[][] params = new Object[productName.length][7];
			for (int i = 0; i < productName.length; i++) {
				params[i][0] = buyId;
				params[i][1] = productName[i];
				params[i][2] = model[i];
				params[i][3] = num[i];
				params[i][4] = unitPrice[i];
				params[i][5] = num[i];
				params[i][6] = unitPrice[i];
			}
			BsBuyApplyService.me.saveDetail(buyId, params, productName.length);
		}
		renderText("success");
	}
	
	@Before(Tx.class)
	public void delete(){
		String buyIds = getPara("buyIds");
		for (String buyId : buyIds.split(",")) {
			BsBuyApplyService.me.delete(buyId);
			BsBuyApplyService.me.deleteDetail(buyId);
		}
		renderText("success");
	}
}
